/*
 * Copyright 2007 Rob Nielsen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.proxymatic.core;

import com.googlecode.proxymatic.core.util.ListUtil;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

public class RuntimeContext implements Serializable {
    private final List<Object> implementationObjects;
    private final BuildtimeContext buildtimeContext;

    public RuntimeContext(BuildtimeContext buildtimeContext, Object implementationObject) {
        this(buildtimeContext, new Object[]{implementationObject});
    }

    public RuntimeContext(BuildtimeContext buildtimeContext, Object... implementationObjects) {
        this.buildtimeContext = buildtimeContext;
        this.implementationObjects = ListUtil.asProtectedList(implementationObjects);
        validate();
    }

    public Object getImplementationObject() {
        if (implementationObjects.size() != 1) {
            throw new IllegalArgumentException("Expected 1 but found " + implementationObjects.size() + " implementation objects");
        }
        return implementationObjects.get(0);
    }

    public Object getImplementationObject(int implementationIndex) {
        return implementationObjects.get(implementationIndex);
    }

    public List<Object> getImplementationObjects() {
        return implementationObjects;
    }

    public BuildtimeContext getBuildtimeContext() {
        return buildtimeContext;
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        RuntimeContext that = (RuntimeContext) o;
        if (!buildtimeContext.equals(that.buildtimeContext)) {
            return false;
        }
        return implementationObjects.equals(that.implementationObjects);
    }

    public boolean sameImplementations(RuntimeContext runtimeContent) {
        if (!buildtimeContext.equals(runtimeContent.buildtimeContext) || implementationObjects.size() != runtimeContent.implementationObjects.size()) {
            return false;
        }
        Iterator otherObjectsIterator = runtimeContent.implementationObjects.iterator();
        for (Iterator<Object> iterator = implementationObjects.iterator(); iterator.hasNext();) {
            if (iterator.next() != otherObjectsIterator.next()) {
                return false;
            }
        }
        return true;
    }

    public int hashCode() {
        int result;
        result = implementationObjects.hashCode();
        result = 31 * result + buildtimeContext.hashCode();
        return result;
    }

    // FIX ROB Test this.
    /**
     * Checks that if the buildtimecontext says that implementation instances will be available at runtime, they are.
     */
    private void validate() {
        boolean runtime = buildtimeContext.isImplementationAvailableAtRuntime();
        if (runtime) {
            List<Class> implementationClasses = buildtimeContext.getImplementationClasses();
            if (implementationClasses.size() != implementationObjects.size()) {
                throw new IllegalArgumentException("Buildtime and runtime contexts do not match: " + implementationObjects + " do not correspond to " + implementationClasses);
            }
            for (int i = 0; i < implementationClasses.size(); i++) {
                Class implementationClass = implementationClasses.get(i);
                Object implementationObject = implementationObjects.get(i);
                if (!implementationClass.isInstance(implementationObject)) {
                    throw new IllegalArgumentException("Buildtime and runtime contexts do not match: For element " + i + " " + implementationObject + " is not an instance of " + implementationClass);
                }
            }
        } else {
            if (implementationObjects.size() > 0) {
                throw new IllegalArgumentException("Implementation objects were supplied but the buildtime context said they wouldn't");
            }
        }
    }
}
